package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/qryGoods")
    public JSON  qryGoods(){
        List<Map<String, Object>> maps = goodsService.qryGoods();
        JSONObject rs = new JSONObject();
        rs.put("data",maps);
        return rs;
    }

    @RequestMapping("/qryGoodsNeed")
    public JSON  qryNeedGoods(@RequestParam("orgid")String orgid){
        List<Map<String, Object>> maps = goodsService.qryGoodsNeed(orgid);
        JSONObject rs = new JSONObject();
        rs.put("data",maps);
        return rs;
    }
    @RequestMapping("/updategoodsneed")
    public JSON updategoodsneed(@RequestBody JSONObject updateInfo){
        System.out.println(updateInfo);
//        {"newgoodsneed":["冬装","椅子"],"orgid":"2Ab2db22-77c5-4cb4-8f23-3aaf6f56d905"}
        JSONObject rs = new JSONObject();
        Map<String,Object> map = new HashMap<>();
        map.put("orgid",updateInfo.get("orgid"));
        goodsService.clearneed(map); //更新前清空
        List<String> goods = new ArrayList<>();
        goods = (List<String>) updateInfo.get("newgoodsneed");
        for (String good : goods) {
            map.put("goodsname",good);
            System.out.println("更新goodsneed表:"+map);
            goodsService.updategoodsneed(map);
        }

        return rs;
    }

    @RequestMapping("/editGood")
    public JSON editGood(@RequestBody JSONObject editGoodInfo) {
        JSONObject rs = new JSONObject();
        System.out.println("修改物品信息" + editGoodInfo);
        //{id: 'QWEZX', goodname: '3', goodpoint: null, oldName: '书籍'}
        Map<String, Object> map = new HashMap<>();
        map.put("id", editGoodInfo.get("id"));
        map.put("oldName", editGoodInfo.get("oldName"));
        if (editGoodInfo.get("goodpoint") != null && editGoodInfo.get("goodpoint") != "") {
//            map.put("goodpoint",editGoodInfo.get("goodpoint"));
            map.put("goodpoint", Integer.parseInt((String) editGoodInfo.get("goodpoint")));
        }
        if (editGoodInfo.get("goodname") != null && editGoodInfo.get("goodname") != "") {
            map.put("goodname", editGoodInfo.get("goodname"));
        }
        int i = goodsService.editGood(map);
        if (i > 0) {
            rs.put("state", "0");
            rs.put("message", "更新成功!");
        } else {
            rs.put("state", "1");
            rs.put("message", "操作失败!");
        }
        return rs;
    }
        @RequestMapping("/addGood")
        public JSON addGood(@RequestBody JSONObject addGoodInfo){
            JSONObject rs = new JSONObject();
            System.out.println("新增物品信息"+addGoodInfo);
//        修改物品信息{"goodpoint":  "100","goodname":"123"}
            addGoodInfo.put("id", UUID.randomUUID().toString());
            int i = goodsService.addGood(addGoodInfo);
            if (i>0){
                rs.put("state","0");
                rs.put("message","操作成功!");
            }else{
                rs.put("state","1");
                rs.put("message","操作失败!");
            }
            return rs;
    }

    @RequestMapping("/qryType")
    public JSON qryType(){
        JSONObject rs =  new JSONObject();
        List<Map<String, Object>> maps = goodsService.qryType();
        for (Map<String, Object> map : maps) {
            List<Map<String, Object>> children = goodsService.qryTypeDetail((String) map.get("fatherid"));
            map.put("children",children);
        }
        rs.put("data",maps);
        return rs;
    }
    @RequestMapping("editFatherTypeName")
    public JSON editType(@RequestBody JSONObject editInfo){
        JSONObject rs =  new JSONObject();
        System.out.println("修改父类信息"+editInfo);
        if (editInfo.get("newName")==null ||editInfo.get("newNmae")==""){
            rs.put("state","2");
            rs.put("message","请输入新名称");
            return rs;
        }
//        {"newName":"新手机","fatherid":"AAA"}
        int i = goodsService.editFatherTypeName(editInfo);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
        }
        return rs;
    }
    @RequestMapping("/addFatherType")
    public JSON addFatherType(@RequestParam("fatherName")String fatherName){
        JSONObject rs = new JSONObject();
        System.out.println("新增父类别名称:"+fatherName);
        Map<String,Object> map =  new HashMap<>();
        map.put("fathername",fatherName);
        map.put("fatherid",UUID.randomUUID().toString());
        int i = goodsService.addFatherType(map);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
        }
        return rs;
    }

    @RequestMapping("/delFatherType")
    public JSON delFatherType(@RequestParam("fatherId")String fatherId){
        System.out.println("要删除的父类ID："+fatherId);
        JSONObject rs = new JSONObject();
        int i = goodsService.delFatherType(fatherId);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
        }
        return rs;
    }

    @RequestMapping("editChildTypeName")
    public JSON editChildTypeName(@RequestBody JSONObject editInfo){
        JSONObject rs =  new JSONObject();
        System.out.println("修改子类信息"+editInfo);
        //{childid: 'AAA2', newChildname: '123', oldChildName: '平板'}
        if (editInfo.get("newChildname")==null ||editInfo.get("newChildname")==""){
            rs.put("state","2");
            rs.put("message","请输入新名称");
            return rs;
        }
        int i = goodsService.editChildTypeName(editInfo);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
        }
        return rs;
    }

    @RequestMapping("/delChildType")
    public JSON delChildType(@RequestParam("childid")String childid){
        System.out.println("要删除的子类ID："+childid);
        JSONObject rs = new JSONObject();
        int i = goodsService.delChildType(childid);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
        }
        return rs;
    }

    @RequestMapping("/addChildTypeName")
    public JSON addChildTypeName(@RequestBody JSONObject newChildInfo){
        JSONObject rs = new JSONObject();
        newChildInfo.put("childid",UUID.randomUUID().toString());
        System.out.println("新增子类:"+newChildInfo);
//格式:{"fatherid":"AAA","childname":"手机","childid":"bb5088fb-9477-478c-99aa-fc43f3078f34"}
        newChildInfo.put("childid",UUID.randomUUID().toString());

        int i = goodsService.addChildTypeName(newChildInfo);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
        }
        return rs;
    }
}
